package br.ufrj.dcc.xml.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import br.ufrj.dcc.xml.ActionConstants;
import br.ufrj.dcc.xml.database.DatabaseSettings;

public class ConnectionForm extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JPanel jPanelLabels = null;

	private JPanel jPanelButtons = null;

	private JPanel jPanelTexts = null;

	private JButton jButtonConnect = null;

	private JButton jButtonCancel = null;

	private JLabel labelHost = null;

	private JLabel labelSchema = null;

	private JLabel labelUsername = null;

	private JLabel labelPassword = null;

	private JTextField textFieldHost = null;

	private JTextField textFieldSchema = null;

	private JTextField textFieldUserName = null;

	private JPasswordField textFieldPassword = null;

	/**
	 * This method initializes jPanelLabels
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelLabels() {
		if (jPanelLabels == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setAlignment(java.awt.FlowLayout.LEFT);
			flowLayout.setVgap(8);
			flowLayout.setHgap(5);
			labelPassword = new JLabel();
			labelPassword.setText("Password");
			labelUsername = new JLabel();
			labelUsername.setText("UsertName");
			labelSchema = new JLabel();
			labelSchema.setText("Schema");
			labelHost = new JLabel();
			labelHost.setText("Host");
			jPanelLabels = new JPanel();
			jPanelLabels.setLayout(flowLayout);
			jPanelLabels.setPreferredSize(new Dimension(80, 100));
			jPanelLabels.add(labelHost, null);
			jPanelLabels.add(labelSchema, null);
			jPanelLabels.add(labelUsername, null);
			jPanelLabels.add(labelPassword, null);
		}
		return jPanelLabels;
	}

	/**
	 * This method initializes jPanelButtons
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelButtons() {
		if (jPanelButtons == null) {
			jPanelButtons = new JPanel();
			jPanelButtons.setLayout(new FlowLayout());
			jPanelButtons.setPreferredSize(new Dimension(100, 50));
			jPanelButtons.add(getJButtonOk(), null);
			jPanelButtons.add(getJButtonCancel(), null);
		}
		return jPanelButtons;
	}

	/**
	 * This method initializes jPanelTexts
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelTexts() {
		if (jPanelTexts == null) {
			FlowLayout flowLayout1 = new FlowLayout();
			flowLayout1.setVgap(5);
			flowLayout1.setAlignment(java.awt.FlowLayout.LEFT);
			jPanelTexts = new JPanel();
			jPanelTexts.setLayout(flowLayout1);
			jPanelTexts.add(getTextFieldHost(), null);
			jPanelTexts.add(getTextFieldSchema(), null);
			jPanelTexts.add(getTextFieldUserName(), null);
			jPanelTexts.add(getTextFieldPassword(), null);
		}
		return jPanelTexts;
	}

	/**
	 * This method initializes jButtonOk
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonOk() {
		if (jButtonConnect == null) {
			jButtonConnect = new JButton();
			jButtonConnect.setText("Conectar");
			jButtonConnect.setPreferredSize(new Dimension(90, 26));
		}
		return jButtonConnect;
	}

	/**
	 * This method initializes jButtonCancel
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			jButtonCancel.setText("Cancelar");
			jButtonCancel.setPreferredSize(new Dimension(90, 26));
		}
		return jButtonCancel;
	}

	/**
	 * This method initializes textFieldHost
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTextFieldHost() {
		if (textFieldHost == null) {
			textFieldHost = new JTextField();
			textFieldHost.setText("");
			textFieldHost.setPreferredSize(new Dimension(150, 20));
		}
		return textFieldHost;
	}

	/**
	 * This method initializes textFieldSchema
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTextFieldSchema() {
		if (textFieldSchema == null) {
			textFieldSchema = new JTextField();
			textFieldSchema.setPreferredSize(new Dimension(150, 20));
			textFieldSchema.setText("");
		}
		return textFieldSchema;
	}

	/**
	 * This method initializes textFieldUserName
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTextFieldUserName() {
		if (textFieldUserName == null) {
			textFieldUserName = new JTextField();
			textFieldUserName.setPreferredSize(new Dimension(150, 20));
			textFieldUserName.setText("");
		}
		return textFieldUserName;
	}

	/**
	 * This method initializes textFieldPassword
	 * 
	 * @return javax.swing.JTextField
	 */
	private JPasswordField getTextFieldPassword() {
		if (textFieldPassword == null) {
			textFieldPassword = new JPasswordField();
			textFieldPassword.setPreferredSize(new Dimension(150, 20));
			textFieldPassword.setText("");
		}
		return textFieldPassword;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				ConnectionForm thisClass = new ConnectionForm();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	/**
	 * This is the default constructor
	 */
	public ConnectionForm(DatabaseSettings dbs) {
		super();
		initialize();
		loadFromDatabaseSettings(dbs);
	}

	public ConnectionForm() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setResizable(false);
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setContentPane(getJContentPane());
		this.setTitle("Conectando ao banco");

		// Centraliza..
		Dimension us = this.getSize(), them = Toolkit.getDefaultToolkit()
				.getScreenSize();
		int newX = (them.width - us.width) / 2;
		int newY = (them.height - us.height) / 2;
		this.setLocation(newX, newY);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanelLabels(), BorderLayout.WEST);
			jContentPane.add(getJPanelButtons(), BorderLayout.SOUTH);
			jContentPane.add(getJPanelTexts(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	public void setListener(ActionListener listener) {
		jButtonCancel.addActionListener(listener);
		jButtonCancel.setActionCommand(Integer
				.toString(ActionConstants.CANCELAR_CONECTAR));

		jButtonConnect.addActionListener(listener);
		jButtonConnect.setActionCommand(Integer
				.toString(ActionConstants.OK_CONECTAR));
	}

	public void loadFromDatabaseSettings(DatabaseSettings dbS) {
		textFieldHost.setText(dbS.getHost());
		textFieldSchema.setText(dbS.getSchema());
		textFieldUserName.setText(dbS.getUser());
		textFieldPassword.setText(dbS.getPassword());

	}

	@SuppressWarnings("deprecation")
	public void storeOnDatabaseSettings(DatabaseSettings dbS) {
		dbS.setHost(textFieldHost.getText());
		dbS.setSchema(textFieldSchema.getText());
		dbS.setUser(textFieldUserName.getText());
		dbS.setPassword(textFieldPassword.getText());

	}
}
